package com.bakend.modules.sys.controller;

import com.bakend.common.R;
import com.bakend.modules.sys.entity.User;
import com.bakend.modules.sys.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class LoginAndRigisterController {

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public R login(@RequestBody User user){
        String token = userService.login(user.getUserCode(), user.getPwd());
        if (token == null) {
            return R.Error().setMsg("用户名或密码错误");
        }

        return R.Ok().setData(token);

    }
    @PostMapping("/regiter")
    public R register(@RequestBody User user) throws Exception {
        userService.register(user.getUserCode(), user.getPwd(),user.getUserName());
        return R.Ok().setMsg("注册成功");

    }
}
